package com.espoir.shp.read02;

import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Geometry;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
//import com.vividsolutions.jts.geom.Geometry;

public class CoordinateUitls {
	/**
	 * WGS84转换albers
	 * @return
	 * @author ygc
	 * @throws IOException 
	 * @throws FactoryException
	 * @throws TransformException
	 * @throws MismatchedDimensionException
	 */
	public static String WGS84ToAlbers(String geometry) throws IOException, FactoryException, MismatchedDimensionException, TransformException {
		GeometryJSON geometryJSON2 = new GeometryJSON(15);
		Geometry geometry2 = geometryJSON2.read(new StringReader(geometry.toString()));

//				com.alibaba.fastjson.JSONObject temp = JSON.parseObject(geometry);
		// 这里是以OGC WKT形式定义的是World Mercator投影，网页地图一般使用该投影
		final String strWKTMercator = 
				"PROJCS[\"World_Mercator\","
				+ "GEOGCS[\"GCS_WGS_1984\"," 
				+ "DATUM[\"WGS_1984\","
				+ "SPHEROID[\"WGS_1984\",6378137,298.257223563]]," 
				+ "PRIMEM[\"Greenwich\",0],"
				+ "UNIT[\"Degree\",0.017453292519943295]]," 
				+ "PROJECTION[\"Albers\"],"
				+ "PARAMETER[\"false_easting\",0]," 
				+ "PARAMETER[\"false_northing\",0],"
				+ "PARAMETER[\"central_meridian\",105.0]," 
				+ "PARAMETER[\"standard_parallel_1\",25.0],"
				+ "PARAMETER[\"standard_parallel_2\",47.0]," 
				+ "PARAMETER[\"latitude_of_origin\",0],"
				+ "UNIT[\"Meter\",1]]";
		/*
		 * CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator);
		 * CRS.decode("EPSG:3857"); xfa1
		 */
		CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator);
		// 做投影转换，将WCG84坐标转换成世界墨卡托投影转 
		MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84,mercatroCRS);
		Geometry targetPoint = (Geometry) JTS.transform(geometry2, transform);
		StringWriter writer = new StringWriter();

		geometryJSON2.write(targetPoint, writer);
		writer.flush();
		writer.close();
		return writer.toString();
	}
	
	/**
	 * albers转WGS84
	 * @return
	 * @author ygc
	 * @throws IOException 
	 * @throws FactoryException
	 * @throws TransformException
	 * @throws MismatchedDimensionException
	 */
	public static String AlbersToWGS84(String geometry) throws IOException, FactoryException, MismatchedDimensionException, TransformException {
		GeometryJSON geometryJSON2 = new GeometryJSON(15);
		Geometry geometry2 = geometryJSON2.read(new StringReader(geometry.toString()));

//				com.alibaba.fastjson.JSONObject temp = JSON.parseObject(geometry);
		// 这里是以OGC WKT形式定义的是World Mercator投影，网页地图一般使用该投影
		final String strWKTMercator = 
				"PROJCS[\"World_Mercator\","
				+ "GEOGCS[\"GCS_WGS_1984\"," 
				+ "DATUM[\"WGS_1984\","
				+ "SPHEROID[\"WGS_1984\",6378137,298.257223563]]," 
				+ "PRIMEM[\"Greenwich\",0],"
				+ "UNIT[\"Degree\",0.017453292519943295]]," 
				+ "PROJECTION[\"Albers\"],"
				+ "PARAMETER[\"false_easting\",0]," 
				+ "PARAMETER[\"false_northing\",0],"
				+ "PARAMETER[\"central_meridian\",105.0]," 
				+ "PARAMETER[\"standard_parallel_1\",25.0],"
				+ "PARAMETER[\"standard_parallel_2\",47.0]," 
				+ "PARAMETER[\"latitude_of_origin\",0],"
				+ "UNIT[\"Meter\",1]]";
		/*
		 * CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator);
		 * CRS.decode("EPSG:3857"); xfa1
		 */
		CoordinateReferenceSystem mercatroCRS = CRS.parseWKT(strWKTMercator);
		// 做投影转换，将WCG84坐标转换成世界墨卡托投影转 
		MathTransform transform = CRS.findMathTransform(mercatroCRS, DefaultGeographicCRS.WGS84);
		Geometry targetPoint = (Geometry) JTS.transform(geometry2, transform);
		StringWriter writer = new StringWriter();

		geometryJSON2.write(targetPoint, writer);
		writer.flush();
		writer.close();
		return writer.toString();
	}
}
